package my.graduation.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import my.graduation.dto.ClinicInfoDto;
import my.graduation.dto.DoctorInfoDto;
import my.graduation.dto.ExamItemDto;
import my.graduation.entity.Clinic;
import my.graduation.entity.Department;
import my.graduation.entity.Doctor;
import my.graduation.entity.ExaminationItem;
import my.graduation.enums.HttpCodeEnums;
import my.graduation.mapper.ClinicMapper;
import my.graduation.mapper.DepartmentMapper;
import my.graduation.mapper.ExaminationItemMapper;
import my.graduation.result.Result;
import my.graduation.service.ExaminationItemService;
import my.graduation.vo.DoctorVO;
import my.graduation.vo.ExaminationItemVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author: RavenLyn
 * @Date: 2025-02-26
 */
@Service
public class ExaminationItemServiceImpl extends ServiceImpl<ExaminationItemMapper, ExaminationItem> implements ExaminationItemService {
    /**
     * 获取所有检查项目信息
     * @returns {*}
     */
    @Override
    public Result<List<ExaminationItem>> getList() {
        return Result.success(list());
    }

    /**
     * 根据条件(clinicId)获取检查项目
     * @param dto
     * @return
     */
    @Override
    public Result<List<ExaminationItem>> getListByCon(ExamItemDto dto) {
        if(dto.getClinicId() != null){
            List<ExaminationItem> list = list(Wrappers.<ExaminationItem>lambdaQuery().eq(ExaminationItem::getClinicId, dto.getClinicId()));
            return Result.success(list);
        }else {
            return Result.error(HttpCodeEnums.DATA_NOT_EXIST);
        }
    }

    /**
     * 医生获取所有检查项目信息itemList
     * @return
     */
    @Override
    public Result<List<ExaminationItem>> getAllItemList() {
        return Result.success(list());
    }

    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private ClinicMapper clinicMapper;

    /**
     * 获取检查项目列表
     * @return
     */
    @Override
    public Result<List<ExaminationItemVO>> getVOList() {
        // 1 获取doctorList
        List<ExaminationItem> examItemList = list();

        return getListMethod(examItemList);
    }

    /**
     * 条件获取检查项目列表
     * @param dto
     * @return
     */
    @Override
    public Result<List<ExaminationItemVO>> getVOListByCon(DoctorInfoDto dto) {
        // 1 获取doctorList
        List<ExaminationItem> examItemList = list(Wrappers.<ExaminationItem>lambdaQuery()
                .eq(dto.getDepartmentId() != null, ExaminationItem::getDepartmentId, dto.getDepartmentId())
                .eq(dto.getClinicId() != null, ExaminationItem::getClinicId, dto.getClinicId())
                .like(dto.getName() != null,ExaminationItem::getName,dto.getName()));

        return getListMethod(examItemList);
    }

    /**
     * 获取检查项目列表信息封装method
     * @param examItemList
     * @return
     */
    public Result<List<ExaminationItemVO>> getListMethod(List<ExaminationItem> examItemList){
        // 2 从examItemList中提取所有的departmentId
        List<Integer> departIdList = examItemList.stream()
                .map(ExaminationItem::getDepartmentId)
                .collect(Collectors.toList());
        // 从examItemList中提取所有的clinicId
        List<Integer> clinicIdList = examItemList.stream()
                .map(ExaminationItem::getClinicId)
                .collect(Collectors.toList());

        // 3 查询出 departmentList 和 clinicList
        List<Department> departmentList = departmentMapper.selectBatchIds(departIdList);
        List<Clinic> clinicList = clinicMapper.selectBatchIds(clinicIdList);

        // 4 封装成map，提高查询性能
        Map<Integer, String> departNameMap = departmentList.stream()
                .collect(Collectors.toMap(Department::getId, Department::getName));
        Map<Integer, String> clinicNameMap = clinicList.stream()
                .collect(Collectors.toMap(Clinic::getId, Clinic::getName));

        // 5 装到clinicVO中
        List<ExaminationItemVO> examItemVOList = new ArrayList<>();
        for (ExaminationItem examItem : examItemList){
//            System.out.println(examItem.getClinicId());
            // 使用map集合，将O(n)降低成O(1)
            String departmentName = departNameMap.getOrDefault(examItem.getDepartmentId(),null);
            String clinicName = clinicNameMap.getOrDefault(examItem.getClinicId(),null);
            examItemVOList.add(new ExaminationItemVO(examItem, departmentName,clinicName));
        }

        return Result.success(examItemVOList);
    }

    /**
     * 新增检查项目
     * @param dto
     * @return
     */
    @Override
    public Result add(ExamItemDto dto) {

        // 2 校验不允许空的字段
        if(StringUtils.isNotBlank(dto.getName()) &&
                dto.getFee() != null &&
                dto.getDepartmentId() != null &&
                dto.getClinicId() != null){

            ExaminationItem examItem = new ExaminationItem();
            BeanUtils.copyProperties(dto,examItem);

            boolean i = save(examItem);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else {
            return Result.error(HttpCodeEnums.INFO_IS_BLANK);
        }
    }

    /**
     * 修改更新检查项目
     * @param dto
     * @return
     */
    @Override
    public Result update(ExamItemDto dto) {
        // 校验不允许空的字段
        if(dto.getId() != null){
            ExaminationItem examItem = new ExaminationItem();
            BeanUtils.copyProperties(dto,examItem);
            boolean i = updateById(examItem);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else {
            return Result.error(HttpCodeEnums.DATA_NOT_EXIST);
        }
    }

    /**
     * 删除检查项目
     * @param dto
     * @return
     */
    @Override
    public Result delete(ExamItemDto dto) {
        // 校验不允许空的字段
        if(dto.getId() != null){
            ExaminationItem examItem = new ExaminationItem();
            BeanUtils.copyProperties(dto,examItem);
            boolean i = removeById(examItem);
            if (i) {
                return Result.success();
            } else {
                return Result.error(HttpCodeEnums.SYSTEM_ERROR);
            }
        }else {
            return Result.error(HttpCodeEnums.DATA_NOT_EXIST);
        }
    }

    /**
     * 获取该检查项目的注意事项
     * @param dto
     * @return
     */
    @Override
    public Result<ExaminationItem> getItem(ExamItemDto dto) {
        if(dto.getId() != null){
            ExaminationItem one = getOne(Wrappers.<ExaminationItem>lambdaQuery()
                    .eq(ExaminationItem::getId, dto.getId()));
            return Result.success(one);
        }else {
            return Result.error(HttpCodeEnums.DATA_NOT_EXIST);
        }
    }
}
